Advanced Debugging
About AdvDbg Consult Train Services Products Tools Community Contact  
欢迎光临 高端调试 登录 | 注册 | FAQ
 
  ACPI调试
Linux内核调试
Windows内核调试
 
  调试战役
调试原理
新工具观察
 
  Linux
Windows Vista
Windows
 
  Linux驱动
WDF
WDM
 
  PCI Express
PCI/PCI-X
USB
无线通信协议
 
  64位CPU
ARM
IA-32
  CPU Info Center
 
  ACPI标准
系统认证
Desktop
服务器
 
  Embedded Linux
嵌入式开发工具
VxWorks
WinCE
嵌入式Windows
 
  格蠹调试套件(GDK)
  格蠹学院
  小朱书店
  老雷的微博
  《软件调试》
  《格蠹汇编》
  《软件调试(第二版)》
沪ICP备11027180号-1

Windows内核调试

帖子发起人: ala   发起时间: 2008-10-08 18:35 下午   回复: 7

Print Search
帖子排序:    
   2008-10-08, 18:35 下午
x_b_q 离线,最后访问时间: 2010/6/13 13:24:38 ala

发帖数前500位
注册: 2008-10-08
发 贴: 3
Hook NtCreateMutant 时OBJECT_ATTRIBUTES 总是有问题
Reply Quote
为什么也总是发现0xc0000005 错误? 但是不用ObjectAttributesNew 而用传进来的原始ObjectAttributes就没问题?

OBJECT_ATTRIBUTES ObjectAttributesNew;

ObjectAttributesNew=*ObjectAttributes;
////
ntStatus = ((TYPE_NtCreateMutant)g_pvOriginNtCreateMutant) (
MutantHandle,DesiredAccess,&ObjectAttributesNew,InitialOwner
);

DbgPrint("[%d] StubNtCreateMutant wsNameNew:%ws ntStatus:0x%x SecurityDescriptor:0x%x RootDirectory:0x%x pObjectName:0x%x",
KMGetCurrentProcessId(),wsNameNew,ntStatus,
ObjectAttributes->SecurityDescriptor,
ObjectAttributes->RootDirectory,
ObjectAttributes->ObjectName
);
IP 地址: 已记录   报告
   2008-10-08, 18:38 下午
x_b_q 离线,最后访问时间: 2010/6/13 13:24:38 ala

发帖数前500位
注册: 2008-10-08
发 贴: 3
纠正描述
Reply Quote
为什么也总是发现0xc0000005 错误? 但是不用ObjectAttributesNew 而用传进来的原始ObjectAttributes就没问题?

OBJECT_ATTRIBUTES ObjectAttributesNew;

ObjectAttributesNew=*ObjectAttributes;
////
ntStatus = ((TYPE_NtCreateMutant)g_pvOriginNtCreateMutant) (
MutantHandle,DesiredAccess,&ObjectAttributesNew,InitialOwner
);

DbgPrint("[%d] StubNtCreateMutant wsNameNew:%ws ntStatus:0x%x SecurityDescriptor:0x%x RootDirectory:0x%x pObjectName:0x%x",
KMGetCurrentProcessId(),wsNameNew,ntStatus,
ObjectAttributes->SecurityDescriptor,
ObjectAttributes->RootDirectory,
ObjectAttributes->ObjectName
);
IP 地址: 已记录   报告
   2008-10-08, 22:19 下午
Raymond 离线,最后访问时间: 2020/7/3 3:40:25 格蠹老雷

发帖数前10位
注册: 2005-12-19
发 贴: 1,303
Re: 纠正描述
Reply Quote
OBJECT_ATTRIBUTES结构是所谓的不透明结构(opaque structure),因此是不可以像你代码里那样直接将一个结构赋给另一个结构的。

IP 地址: 已记录   报告
   2008-10-09, 01:24 上午
MJ0011 离线,最后访问时间: 2009/12/24 22:33:41 MJ0011

发帖数前10位
注册: 2008-04-24
发 贴: 112
Re: 纠正描述
Reply Quote
RltCopyMemory(ObjectAttributesNew , &ObjectAttributes , sizeof(ObjectAttributesNew ));
IP 地址: 已记录   报告
   2008-10-09, 01:28 上午
MJ0011 离线,最后访问时间: 2009/12/24 22:33:41 MJ0011

发帖数前10位
注册: 2008-04-24
发 贴: 112
Re: 纠正描述
Reply Quote
另外,楼主出现的问题原因是这样的:

你直接调用了原始的g_pvOriginNtCreateMutant,此时你的线程前个模式还是UserMode,而你的ObjectAttributesNew 是在内核空间的堆栈变量,在前个模式是UserMode,Nt*等函数会对传入的参数地址做ProbeForRead 操作,拒绝内核地址,因此会返回0xc0000005

解决方法是调用手动将前个模式该成KernelMode,调用后改回

或者直接调用ZwCreateMutant,让KiSystemService为你做模式转换。

不过前提是你要对所有你传递给函数的,由RING3传递上来的参数做详细检查,因为KernelMode下调用不会对你的参数做任何检查的,如果你不检查,就会引发内核拒绝服务(KERNEL DOS) 攻击漏洞的可能。
IP 地址: 已记录   报告
   2008-11-04, 00:28 上午
x_b_q 离线,最后访问时间: 2010/6/13 13:24:38 ala

发帖数前500位
注册: 2008-10-08
发 贴: 3
Re: Hook NtCreateMutant 时OBJECT_ATTRIBUTES 总是有问题
Reply Quote
解决方法是调用手动将前个模式该成KernelMode


----这个怎么做啊?

另:在驱动里面怎么调用ZwCreateMutant?取得ntdll的ZwCreateMutant?要是NtCreateMutant的调用者是驱动会不会有问题?
IP 地址: 已记录   报告
   2008-11-04, 10:29 上午
WANGyu 离线,最后访问时间: 2012/9/10 3:34:00 王宇

发帖数前10位
男
注册: 2007-05-08
发 贴: 306
Re: Hook NtCreateMutant 时OBJECT_ATTRIBUTES 总是有问题
Reply Quote
马甲同学的意思就是在调试器里将Call之前的压栈参数手工由 1 改为 0。

楼主请看看我整理的这篇文章:《Nt vs. Zw - Clearing Confusion on the Native API》(http://advdbg.org/dfstore/zip/Nt%20vs.%20Zw%20-%20Clearing%20Confusion%20on%20the%20Native%20API.pdf)

里面有这么一段:

The NtXxxx version of the native system service is the name of the function
itself. Thus, when a Kernel Mode component calls the NtXxxx version of the
system service, whatever is presently set into previous mode is unchanged.
Thus, it is quite possible that the Kernel component could be running on an
arbitrary User stack, with the requestor mode set to User. The system service
will not know any better, attempt to validate the request parameters, possibly
using the credentials of the arbitrary User Mode thread, and thus possibly fail
the request. Another problem here is that one step in the validation process
for a User Mode request is that all passed in buffers have either ProbeForRead
or ProbeForWrite executed on them, depending on the buffer's usage. These
routines raise exceptions if executed on Kernel Mode addresses. Therefore,
if you pass in Kernel Mode buffers with your request mode set to User, your
calls into the native API return STATUS_ACCESS_VIOLATION.

注意到最后面的错误号:STATUS_ACCESS_VIOLATION,再查一下“ntstatus.h”:

//
// MessageId: STATUS_ACCESS_VIOLATION
//
// MessageText:
//
// The instruction at "0x%08lx" referenced memory at "0x%08lx". The memory could not be "%s".
//
#define STATUS_ACCESS_VIOLATION ((NTSTATUS)0xC0000005L) // winnt


文字很简单,就不多翻译了。关于 Native API 的 Previous Mode 有一段很好的总结如下:

If you are in User Mode, use whatever variant (指 Zw* 或 Nt* 的 Native API) you think makes your code look pretty (呵呵). In Kernel Mode, use the ZwXxx routines and get your previous mode set properly (楼主的问题就在这里了), to Kernel Mode.
IP 地址: 已记录   报告
   2008-11-05, 18:47 下午
nightxie 离线,最后访问时间: 2010/4/3 8:32:07 nightxie

发帖数前25位
注册: 2008-06-09
发 贴: 43
Re: Hook NtCreateMutant 时OBJECT_ATTRIBUTES 总是有问题
Reply Quote
其实就是NtCreateMutant中有这个东西。。。
try {
PreviousMode = KeGetPreviousMode();
if (PreviousMode != KernelMode) {
ProbeForWriteHandle(MutantHandle);
......

} except(ExSystemExceptionFilter()) {
......

MJ前辈是改Kthread->PreviousMode = 0
IP 地址: 已记录   报告
高端调试 » 软件调试 » Windows内核调试 » Hook NtCreateMutant 时OBJECT_ATTRIBUTES 总是有问题

 
Legal Notice Privacy Statement Corporate Governance Corporate Governance
(C)2004-2020 ADVDBG.ORG All Rights Reserved.